package com.copm.demonstrationsystem.service.introductionfeatures.commons;

import com.copm.demonstrationsystem.commons.response.ResponseStatus;
import com.copm.demonstrationsystem.commons.util.ImageUtils;
import com.copm.demonstrationsystem.dao.introductionfeatures.IntroductionImageDao;
import com.copm.demonstrationsystem.dao.introductionfeatures.IntroductionTextDao;
import com.copm.demonstrationsystem.pojo.commons.Image;
import com.copm.demonstrationsystem.pojo.introductionfeatures.IntroductionImage;
import com.copm.demonstrationsystem.pojo.introductionfeatures.IntroductionText;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.ws.Response;
import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.List;

/**
 * @Author: Yuan Baiyu
 * @Date: Created in 23:54 2018/5/1
 */

@Service
public class MultiImageAndTextHandle {

    private static Logger logger = LoggerFactory.getLogger(MultiImageAndTextHandle.class);

    @Autowired
    IntroductionTextDao introductionTextDao;

    @Autowired
    IntroductionImageDao introductionImageDao;

    /**
     * 添加“一页多图”页面的图片
     *
     * @param file
     * @param imagekind
     * @return
     */
    public synchronized ResponseStatus addIntroductionImage(MultipartFile file, Long imagekind) {
        ResponseStatus status = new ResponseStatus();
        String []paths;
        Image image;
        //新建一个图片对象
        IntroductionImage introductionImage = new IntroductionImage();

        try {
            //保存图片及返回图片的重命名
            paths = ImageUtils.storeAndCompressionImage(ImageUtils.getImageDir(),file);

            if(paths == null){
                throw new RuntimeException("图片存储失败了！");
            }

        } catch (FileNotFoundException e) {
            status.setStatue(0);
            status.setMessage("项目路径无法找到上传图片" + e.toString());
            logger.info("项目路径无法找到上传图片,上传失败！");
            return status;
        }
        //检验文件后缀
        if (ImageUtils.checkSuffix(paths[0]) == false) {
            status.setStatue(0);
            status.setMessage("文件格式出错，请确认后再次上传");
            logger.info("文件格式出错,上传失败！");
            return status;
        }
        //设置图片的属性
        image = new Image(paths[0], paths[1], paths[2]);
        introductionImage.setImageKind(imagekind);
        introductionImage.setImage(image);
        //保存图片对象
        introductionImageDao.save(introductionImage);
        status.setStatue(1);
        return status;
    }

    /**
     * 添加“一页多文本”页面的文本
     *
     * @param content
     * @param textKind
     * @return
     */
    public synchronized ResponseStatus addIntroductionText(String content, Long textKind,Long seq) {
        ResponseStatus status = new ResponseStatus();
        //新建一个文本对象
        IntroductionText introductionText = new IntroductionText();
        introductionText.setTextKind(textKind);
        introductionText.setContent(content);
        introductionText.setSequence(seq);
        //保存文本对象
        introductionTextDao.save(introductionText);

        status.setStatue(1);
        return status;
    }

    /**
     * 删除“一页多图”页面的图片
     *
     * @param id
     * @return
     */
    public synchronized ResponseStatus deleteIntroductionImage(Long id) {
        ResponseStatus status = new ResponseStatus();
        IntroductionImage introductionImage = introductionImageDao.findByImage_Id(id);
        if (null == introductionImage) {
            status.setStatue(0);
            status.setMessage("没有找到待删除图片");
            logger.info("图片组对象没有找到待删除图片信息，删除失败！");
            return status;
        }
        try {
            //从项目图片目录中删除图片
            ImageUtils.delete(ImageUtils.getImageDir(), introductionImage.getImage().getFileName());
        } catch (FileNotFoundException e) {
            status.setStatue(0);
            status.setMessage("项目路径无法找到待删除图片" + e.toString());
            logger.info("物理图片目录没有找到待删除图片，删除失败！");
            return status;
        }

        //通过Image删除图片对象
        introductionImageDao.delete(introductionImage);
        status.setStatue(1);
        return status;
    }

    /**
     * 删除“一页多文本”页面的文本
     *
     * @param id
     * @return
     */
    public synchronized ResponseStatus deleteIntroductionText(Long id, Long textKind) {
        ResponseStatus status = new ResponseStatus();
        List<IntroductionText> texts = introductionTextDao.findByTextKind(textKind);

        //检查传入ID是否为正确文本ID
        boolean temp = false;
        for (IntroductionText list : texts) {
            if (id == list.getId().longValue()) {
                temp = true;
            }
        }
        if (temp == false) {
            status.setStatue(0);
            status.setMessage("传入的文本ID有误，无法匹配");
            return status;
        }

        introductionTextDao.delete(id);

        status.setStatue(1);
        return status;
    }

}
